package com.sven.zuulgateway.config2;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;
import org.springframework.stereotype.Component;

/**
 * 仅处理来自post过滤器引起的异常
 */
// @Component
public class ErrorDealPostFilter extends SendErrorFilter {
    private static Logger logger = LoggerFactory.getLogger(ErrorDealPostFilter.class);

    @Override
    public String filterType() {
        return "error";
    }

    @Override
    public int filterOrder() {
        //大于ErrorFilter的值
        return 30;
    }

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        ZuulFilter failedFilter = (ZuulFilter) ctx.get("failed.filter");
        System.out.println("----ErrorDealPostFilter----error filter----filterType："+failedFilter.filterType());
        if(failedFilter != null && failedFilter.filterType().equals("post")) {
            logger.error("----ErrorDealPostFilter----error filter----仅处理来自post过滤器引起的异常----filterType : {}", failedFilter.filterType());
            return true;
        }
        return false;
    }

    @Override
    public Object run() {
        return super.run();
        // return null;
    }

}
